<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Controller_Profile extends Controller_Abstract implements Controller_Interface {
    
    /**
     * @var Minime_User
     */
    private $user;
    
    public function __construct($getParams, $postParams, $phpInput, $conf) {
        $this->getParams  = $getParams;
        $this->opstParams = $postParams;
        $this->phpInput   = $phpInput;
        
        $this->conf = $conf;
    }
    
    public function execute () {
        
        // REDO This whole method
        
        $userLoader = new Minime_User_Loader($this->conf);
        $user = $userLoader->getUser($this->getParams['username']);
        
        if(empty($user)) {
            throw new Exception("Profile for user '" . $this->getParams['username'] . "' not found!");
            return;
        }
        
        $subsloader = new Minime_User_Subscriptions_Loader($this->conf);
        $subscriptions = $subsloader->getSubscriptions($this->getParams['username']);
        
        $subsloader = new Minime_User_Subscribers_Loader($this->conf);
        $subscribers = $subsloader->getSubscribers($this->getParams['username']);
        
        $atomLoader = new Minime_Atom_Loader($this->conf);
        $feed = $atomLoader->getFeed($this->getParams['username']);
        
        // Wow, havent done a hack like this for ages!
        
        $this->output = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>' . $user->getTitle() . '</title>
 
 <link rel="alternate" href="' . Minime_HrefRenderer::getLdrrAtomHref($this->getParams['username'], $this->conf) . '" type="application/atom+xml" title="Notice feed for ' . $this->getParams['username'] . ' (Atom)"/>
 <link rel="stylesheet" href="http://www.lobstermonster.org/examples-buddycloud/widget/bubbles.css" type="text/css" media="screen" charset="utf-8" />
 
</head>
<body>
 <div id="bodywrapper">
  <div class="thebignoob">
   <p id="geocurrent">&nbsp;</p>
   <h2 id="channel-title">' . $user->getTitle() . '</h2>
   <p id="channel-desc">' . $user->getSubtitle() . '</p>
  </div>
  <div id="topicswrapper">';
        
        $links = array();
        $tmpEntries = $feed->getEntries();
        $order = array();
        $entries = array();
        foreach ($tmpEntries as $key => $entry) {
            
            if($entry->getInReplyTo()== null) {
                if(!isset($order[$key])) {
                    $order[$key] = strtotime($entry->getUpdated()->getUpdated());
                }
                continue;
            }
            
            if(isset($tmpEntries[$entry->getInReplyTo()->getRef()])) {
                $links[$entry->getInReplyTo()->getRef()][] = $key;
                if(!isset($order[$key])) {
                    $order[$entry->getInReplyTo()->getRef()] = strtotime($entry->getUpdated()->getUpdated());
                } else if( $order[$key] < strtotime($entry->getUpdated()->getUpdated()) ) {
                    $order[$entry->getInReplyTo()->getRef()] = strtotime($entry->getUpdated()->getUpdated());
                }
                continue;
            }
            
            if(!isset($order[$key])) {
                $order[$key] = strtotime($entry->getUpdated()->getUpdated());
            }
        }
        
        foreach ($order as $k => $v) {
            
        }
        
        uasort($order, array("Controller_Profile", "cmp_entries"));
        
        foreach ($order as $k => $v) {

            $logo = "";
            $entry = $tmpEntries[$k];
            
            if( $entry->getAuthor()->getName()->getName() == $user->getName() ) {
                $logo = $user->getLogo();
            }
            
            if(empty($logo)) {
                $sub = $subscriptions->getSubscriptionByUri($entry->getAuthor()->getUri()->getUri());
                if($sub) {
                    $logo = $sub->getLogo();
                }
            } 
            
            if(empty($logo)) {
                $sub = $subscribers->getSubscriberByUri($entry->getAuthor()->getUri()->getUri());
                if($sub) {
	                $logo = $sub->getLogo();
                }
            }
            
            if(empty($logo)) {
                $logo = "http://static.open.salon.com/files/default_avatar1234375017.jpg";
            }

            $content = "";
            if($entry->getContent()) {
                $content = $entry->getContent()->getContent();   
            }
            if(empty($content)) {
                $content = $entry->getTitle()->getTitle(); 
            }
            
            $content = html_entity_decode($content);

            $this->output = $this->output . '<div style="display: block;" id="1280902449082" class="topicwrapper">';
            $this->output = $this->output . ' <div class="topic">';
            $this->output = $this->output . '  <div class="topicavatar"><img class="avatar" width="54" src="' . $logo .  '"></div>';
            $this->output = $this->output . '  <div class="topiccontent"><blockquote>' . $content . '</blockquote><cite><strong><a href="' . $entry->getAuthor()->getUri()->getUri() . '">' . $entry->getAuthor()->getName()->getName() . '</a></strong> ~ <span class="topictime" title="' . $entry->getUpdated()->getUpdated() . '">' . $entry->getUpdated()->getUpdated() . '</span></cite></div>';
            $this->output = $this->output . ' </div>';
            $this->output = $this->output . self::getCommentHTML($links, $entry, $tmpEntries);
            $this->output = $this->output . '</div>';
        }
  
        $this->output = $this->output . '</div>
  <h3>Subscribers:</h3>
  <ul>';
   
        foreach ($subscribers->getSubscribers() as $sub) {
            
            $logo = $sub->getLogo();
            if(empty($logo)) {
                $logo = "http://static.open.salon.com/files/default_avatar1234375017.jpg";
            }
            
            $this->output = $this->output . '<li><img class="avatar" width="27" src="' . $logo .  '"><a href="' . $sub->getAlternate() . '">' . $sub->getName() . '</a></li>';
        }
   
  $this->output = $this->output . '</ul>
  <h3>Subscriptions:</h3>
  <ul>';
   
        foreach ($subscriptions->getSubscriptions() as $sub) {
            
            $logo = $sub->getLogo();
            if(empty($logo)) {
                $logo = "http://static.open.salon.com/files/default_avatar1234375017.jpg";
            }
            
            $this->output = $this->output . '<li><img class="avatar" width="27" src="' . $logo .  '"><a href="' . $sub->getAlternate() . '">' . $sub->getName() . '</a></li>';
        }
   
  $this->output = $this->output . '</ul>
  </div>
</body>
</html>
';
        
        $this->headers['Content-Type'] = "text/html";
    }
    
    private static function getCommentHTML($links, $entry, $tmpEntries) {
        $output = "";
        if(isset($links[$entry->getId()->getId()])) {
            foreach ($links[$entry->getId()->getId()] as $id) {
                $output = $output . ' <div class="comments">';
                $output = $output . '   <div style="display: block;" class="bubble"><blockquote><p>' . html_entity_decode($tmpEntries[$id]->getContent()->getContent()) . '</p></blockquote><cite><strong><a href="' . $tmpEntries[$id]->getAuthor()->getUri()->getUri() . '">' . $tmpEntries[$id]->getAuthor()->getName()->getName() . '</a> ~ <span class="topictime" title="' . $tmpEntries[$id]->getUpdated()->getUpdated() . '">' . $tmpEntries[$id]->getUpdated()->getUpdated() . '</span></strong></cite></div>';
                $output = $output . ' </div>';
                $output = $output . self::getCommentHTML($links, $tmpEntries[$id], $tmpEntries);
            }
        }
        return $output;
    }
    
    public static function cmp_entries($a, $b) {
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? +1 : -1;
    }
}
